package com.zb.dao.impl;

import com.zb.dao.BookDao;
import com.zb.pojo.Book;

import java.util.List;

/**
 * @author zb
 * @create 2021-03-04 20:38
 */
public class BookDaoImpl extends BaseDao implements BookDao {
    @Override
    public int addBook(Book book) {
        String sql = "insert into t_book(name,author,price,sales,stock,img_path) values(?,?,?,?,?,?)";
        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath());
    }

    @Override
    public int deleteBookById(Integer id) {
        String sql = "delete from t_book where id=?";
        return update(sql,id);
    }

    @Override
    public int updateBook(Book book) {

        System.out.println("BookDaoImpl程序在【"+Thread.currentThread()+"】");

        String sql = "update t_book set name=?,author=?,price=?,sales=?,stock=?,img_path=? where id=?";
        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath(),book.getId());
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql = "select id,name,author,price,sales,stock,img_path imgPath from t_book where id=?";
        return queryForOne(Book.class,sql,id);
    }

    @Override
    public List<Book> queryBooks() {
        String sql = "select id,name,author,price,sales,stock,img_path imgPath from t_book";
        return queryForList(Book.class,sql);
    }

    @Override
    public int queryBookTotalCount() {
        String sql = "select count(*) from t_book";
        String strInt = String.valueOf(queryForSingleValue(sql));
        int count = Integer.parseInt(strInt);
        return count;
    }

    @Override
    public List<Book> queryPageItems(int begin, int pageSize) {
        String sql = "select id,name,author,price,sales,stock,img_path imgPath from t_book limit ?,?";
        return queryForList(Book.class,sql,begin,pageSize);
    }

    @Override
    public int queryBookTotalCountByPrice(int min, int max) {
        String sql = "select count(*) from t_book where price between ? and ?";
        String strInt = String.valueOf(queryForSingleValue(sql,min,max));
        int count = Integer.parseInt(strInt);
        return count;
    }

    @Override
    public List<Book> queryPageItemsByPrice(int begin, int pageSize, int min, int max) {
        String sql ="select id,name,author,price,sales,stock,img_path imgPath from t_book where price between ? and ? limit ?,?";
        return queryForList(Book.class,sql,min,max,begin,pageSize);
    }
}